home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / kaffe / findClass.c < prev    next >
C/C++ Source or Header  |  1996-02-13  |  3KB  |  158 lines

  1. /*
  2.  * findClass.c
  3.  * Search the CLASSPATH for the given class name.
  4.  *
  5.  * Copyright (c) 1996 Systems Architecture Research Centre,
  6.  *           City University, London, UK.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  12.  */
  13.  
  14. #define ZDBG(s)
  15.  
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <fcntl.h>
  20. #include <string.h>
  21. #include <sys/stat.h>
  22. #include "errors.h"
  23. #include "file.h"
  24. #include "zipfile.h"
  25.  
  26. #define    CLASSPATH    "CLASSPATH"
  27. #define    MAXBUF        256
  28. #define    MAXPATHELEM    16
  29. #define    MAXPATHLEN    256
  30.  
  31. #define    CP_INVALID    0
  32. #define    CP_FILE        1
  33. #define    CP_DIR        2
  34.  
  35. static struct {
  36.     int    type;
  37.     char*    path;
  38. } classpath[MAXPATHELEM+1];
  39.  
  40. static char realClassPath[MAXPATHLEN];
  41.  
  42. void initClasspath(void);
  43.  
  44. /*
  45.  * Find the named class in a directory or zip file.
  46.  */
  47. void
  48. findClass(char* cname)
  49. {
  50.     char* cp;
  51.     int i;
  52.     char buf[MAXBUF];
  53.     int fp;
  54.     struct stat sbuf;
  55.     classFile hand;
  56.     ZipFile zipf;
  57.     ZipDirectory *zipd;
  58.     int r;
  59.     int j;
  60.  
  61.     /* Look for the class */
  62.     for (i = 0; classpath[i].path != 0; i++) {
  63.         switch (classpath[i].type) {
  64.         case CP_FILE:
  65. ZDBG(            printf("Opening zip file %s for %s\n", classpath[i].path, cname); )
  66.             strcpy(buf, cname);
  67.             strcat(buf, ".class");
  68.             fp = open(classpath[i].path, O_RDONLY);
  69.             zipf.fd = fp;
  70.             if (fp < 0 || read_zip_archive (&zipf) != 0) {
  71.                 continue;
  72.             }
  73.             zipd = (ZipDirectory*)zipf.central_directory;
  74.             for (j = 0; j < zipf.count; j++, zipd = ZIPDIR_NEXT(zipd)) {
  75. ZDBG(    printf ("%d: size:%d, name(#%d)%s, offset:%d\n", i, zipd->size,
  76.   zipd->filename_length, ZIPDIR_FILENAME (zipd), zipd->filestart);    )
  77.                 if (strcmp(buf, ZIPDIR_FILENAME(zipd)) == 0) {
  78. ZDBG(                    printf("FOUND!!\n");        )
  79.                     lseek(fp, zipd->filestart, SEEK_SET);
  80.                     hand.size = zipd->size;
  81.                     free(zipf.central_directory);
  82.                     goto found;
  83.                 }
  84.             }
  85.             free(zipd);
  86.             close(fp);
  87.             continue;
  88.  
  89.         case CP_DIR:
  90.             strcpy(buf, classpath[i].path);
  91.             strcat(buf, "/");
  92.             strcat(buf, cname);
  93.             strcat(buf, ".class");
  94.             fp = open(buf, O_RDONLY);
  95.             if (fp < 0 || fstat(fp, &sbuf) < 0) {
  96.                 continue;
  97.             }
  98.             hand.size = sbuf.st_size;
  99.  
  100.         found:
  101.             hand.base = malloc(hand.size);
  102.             hand.buf = hand.base;
  103.             if (hand.buf == 0) {
  104.                 throwException(OutOfMemoryError);
  105.             }
  106.             if (read(fp, hand.buf, hand.size) != hand.size) {
  107.                 abort();
  108.             }
  109.             close(fp);
  110.             readClass(&hand);
  111.             free(hand.base);
  112.             break;
  113.         }
  114.         return;
  115.     }
  116.     throwException(NoClassDefFoundError);
  117. }
  118.  
  119. /*
  120.  * Initialise class path.
  121.  */
  122. void
  123. initClasspath(void)
  124. {
  125.     struct stat sbuf;
  126.     char* cp;
  127.     int i;
  128.     int fd;
  129.  
  130.     cp = getenv(CLASSPATH);
  131.     if (cp == 0) {
  132.         fprintf(stderr, "CLASSPATH is not set!\n");
  133.         exit(1);
  134.     }
  135.     strcpy(realClassPath, cp);
  136.     cp = realClassPath;
  137.  
  138.     for (i = 0; cp != 0 && i < MAXPATHELEM; i++) {
  139.         classpath[i].path = cp;
  140.         cp = strchr(cp, ':');
  141.         if (cp != 0) {
  142.             *cp = 0;
  143.             cp++;
  144.         }
  145.         if (stat(classpath[i].path, &sbuf) < 0) {
  146.             classpath[i].type = CP_INVALID;
  147.         }
  148.         else if (S_ISDIR(sbuf.st_mode)) {
  149.             classpath[i].type = CP_DIR;
  150.         }
  151.         else {
  152.             classpath[i].type = CP_FILE;
  153.         }
  154.     }
  155.     i++;
  156.     classpath[i].path = 0;
  157. }
  158.